package figure8.GUI;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import figure8.Controller;
import figure8.Project;

/**
 * This class sets up the main window and adds the menu, project panel and
 * control panel to it, as well as listening for events from the main window and
 * passing them to the controller.
 */
public class MainWindow implements WindowListener {  
    private JFrame frame;
    private Controller controller;
    
    private MenuBar menuBar;
    private ProjectPanel projectPanel;
    private ControlPanel controlPanel;
    
    TrayIcon trayIcon;
    SystemTray tray;
    
    public MainWindow(Controller controller) {
        this.controller = controller;
        
        // Try to make the GUI look like native GUI:
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Throwable t) {
            // Don't worry if that goes wrong.
        }
        
        this.frame = new JFrame();
        
        // Tell the window not to close the program automatically when the X
        // button of the window is pressed, and add this class as a listener
        // for that window (the window will call functions on this class when
        // certain events to do with the window happen).
        this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.frame.addWindowListener(this);
        this.frame.setIconImage(Toolkit.getDefaultToolkit().getImage("src/figure8/GUI/Fig8-small.png"));
        this.frame.setTitle("Figure8 Songwriting Tool");
        
        // Create a MenuBar and set it as the window's menu bar.
        this.menuBar = new MenuBar(controller);
        this.frame.setJMenuBar(this.menuBar);
        
        // Create a ProjectPanel and a ControlPanel
        this.projectPanel = new ProjectPanel(this.controller);
        this.controlPanel = new ControlPanel(this.controller);
        
        // Use a border layout for the frame and position the project panel in
        // the center and the control panel in the south. This way the control
        // panel will remain a constant height, but the project panel will grow
        // when the window is resized vertically.
        this.frame.setLayout(new BorderLayout());
        this.frame.add(this.projectPanel, BorderLayout.CENTER);
        this.frame.add(this.controlPanel, BorderLayout.SOUTH);
        
        //create the system tray icon
        this.createTrayIcon();
        
        // Pack all the elements in the window together at the specified size
        // and then make everything visible to the user.
        this.frame.pack();
        this.frame.setVisible(true);
    }
    
    // Getters and Setters
    
    public JFrame getFrame() {
        return this.frame;
    }
    
    public MenuBar getMenuBar() {
        return this.menuBar;
    }
    
    public ProjectPanel getProjectPanel() {
        return this.projectPanel;
    }
    
    public ControlPanel getControlPanel() {
        return this.controlPanel;
    }
    
    public void updateTiming (Project project) {
    	this.projectPanel.updateTiming(project);
    	this.frame.pack();
        this.frame.setVisible(true);
        this.controlPanel.stopped();
    }
    
    public void removeTrayIcon() {
    	//Check the SystemTray support
        if (!SystemTray.isSupported()) {
            System.err.println("SystemTray is not supported");
        }
        else {
        	tray.remove(trayIcon);
        }
    }
    
    public void createTrayIcon()
    {
    	//Check the SystemTray support
        if (!SystemTray.isSupported()) {
            System.err.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().
        		getImage("src/figure8/GUI/Fig8-small.png") , "Figure8");
        tray = SystemTray.getSystemTray();
        
        // Create a popup menu components
        MenuItem about = new MenuItem("About");
        about.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        controller.about();
                    }
                });
        //Menu displayMenu = new Menu("Display");
        MenuItem openItem = new MenuItem("Open");
        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	tray.remove(trayIcon);
                System.exit(0);
            }
        });
        
        //Add components to popup menu
        popup.add(about);
        popup.addSeparator();
        //popup.add(displayMenu);
        //displayMenu.add(errorItem);
        popup.add(openItem);
        popup.add(exitItem);
        trayIcon.setPopupMenu(popup);
        
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println("TrayIcon could not be added.");
            return;
        }
    }
    
    // Event handlers
    
    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }
    
    /*
     * When the user tries to close the window with the X button, tell the
     * controller to exit.
     */
    @Override
    public void windowClosing(WindowEvent e) {
        this.controller.exit();
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }
}
